*set the global for data folder (ESS)
global path ...

*add a folder named "output" inside data folder

********************************************************************************
*Figure 5: predicted values of life satisfaction (from 0 to 100) for a man aged 40 in the upper-middle class (with university education) or unskilled working class (without upper-secondary educ.)

*Figure W.7 (web appendix): predicted values of life satisfaction (from 0 to 100) for a man aged 40 in the upper-middle class (with university education) or unskilled working class (without upper-secondary education)

*GB
clear
use "$path\ESS_dataset_2002-2018_ready.dta"

fre YEAR if class5!=. & STFLIFE100!=. & COUNTRY==82601

quietly reg STFLIFE100 class5##YEAR i.SEX01 AGE i.ed_level if COUNTRY==82601 & AGE>=20 & AGE<=60
quietly margins YEAR, at (AGE=40 SEX01=1) by (class5 ed_level) post
parmest, format (estimate min95 max95 %8.3f p %8.1e) saving ("$path\margins_class5.dta", replace)

use "$path\margins_class5.dta", clear

br

split parm, p("." "#")
rename parm1 class5
destring class5, replace
rename parm3 ed_level
destring ed_level, replace
rename parm5 year
destring year, replace

twoway (lowess estimate year if class==1 & ed_level==3, sort lpattern (solid) lcolor(black))(lowess min95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(lowess max95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(lowess estimate year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(black))(lowess min95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray))(lowess max95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray)), graphregion(color(white)) legend(order(1 "Upper-middle class" 4 "Unskilled working class") nobox region(lstyle(none)) size(*1.4)) yscale(range(40 85)) xscale(range(2002 2016) noextend)  xlabel(2002 2007 2012 2016, labsize(*1.4)) ylabel(40(10)85, nogrid labsize(*1.4)) xtitle("")
graph export "$path\output\GB_margins_lowess_confint.png", replace

twoway (connected estimate year if class==1 & ed_level==3, sort lpattern (solid) lcolor(black) msymbol(circle) mcolor(black))(line min95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(line max95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(connected estimate year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(black) msymbol(triangle) mcolor(black))(line min95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray))(line max95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray)), graphregion(color(white)) legend(order(1 "Upper-middle class" 4 "Unskilled working class") nobox region(lstyle(none)) size(*1.4)) yscale(range(40 85)) xscale(range(2002 2016) noextend)  xlabel(2002 2007 2012 2016, labsize(*1.4)) ylabel(40(10)85, labsize(*1.4) nogrid format(%9.0f)) ytitle("") xtitle("")
graph export "$path\output\GB_margins_connected.png", replace


*SE
clear
use "$path\ESS_dataset_2002-2018_ready.dta"

fre YEAR if class5!=. & STFLIFE100!=. & COUNTRY==752


quietly reg STFLIFE100 class5##YEAR i.SEX01 AGE i.ed_level if COUNTRY==752 & AGE>=20 & AGE<=60
quietly margins YEAR, at (AGE=40 SEX01=1) by (class5 ed_level) post
parmest, format (estimate min95 max95 %8.3f p %8.1e) saving ("$path\margins_class5.dta", replace)

use "$path\margins_class5.dta", clear

br

split parm, p("." "#")
rename parm1 class5
destring class5, replace
rename parm3 ed_level
destring ed_level, replace
rename parm5 year
destring year, replace

twoway (lowess estimate year if class==1 & ed_level==3, sort lpattern (solid) lcolor(black))(lowess min95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(lowess max95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(lowess estimate year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(black))(lowess min95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray))(lowess max95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray)), graphregion(color(white)) legend(order(1 "Upper-middle class" 4 "Unskilled working class") nobox region(lstyle(none)) size(*1.4)) yscale(range(40 85)) xscale(range(2002 2016) noextend)  xlabel(2002 2007 2012 2016, labsize(*1.4)) ylabel(40(10)85, nogrid labsize(*1.4)) xtitle("")
graph export "$path\output\SE_margins_lowess_confint.png", replace

twoway (connected estimate year if class==1 & ed_level==3, sort lpattern (solid) lcolor(black) msymbol(circle) mcolor(black))(line min95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(line max95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(connected estimate year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(black) msymbol(triangle) mcolor(black))(line min95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray))(line max95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray)), graphregion(color(white)) legend(order(1 "Upper-middle class" 4 "Unskilled working class") nobox region(lstyle(none)) size(*1.4)) yscale(range(40 85)) xscale(range(2002 2016) noextend)  xlabel(2002 2007 2012 2016, labsize(*1.4)) ylabel(40(10)85, labsize(*1.4) nogrid format(%9.0f)) ytitle("") xtitle("")
graph export "$path\output\SE_margins_connected.png", replace


*NO
clear
use "$path\ESS_dataset_2002-2018_ready.dta"

fre YEAR if class5!=. & STFLIFE100!=. & COUNTRY==578


quietly reg STFLIFE100 class5##YEAR i.SEX01 AGE i.ed_level if COUNTRY==578 & AGE>=20 & AGE<=60
quietly margins YEAR, at (AGE=40 SEX01=1) by (class5 ed_level) post
parmest, format (estimate min95 max95 %8.3f p %8.1e) saving ("$path\margins_class5.dta", replace)

use "$path\margins_class5.dta", clear

br

split parm, p("." "#")
rename parm1 class5
destring class5, replace
rename parm3 ed_level
destring ed_level, replace
rename parm5 year
destring year, replace

twoway (lowess estimate year if class==1 & ed_level==3, sort lpattern (solid) lcolor(black))(lowess min95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(lowess max95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(lowess estimate year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(black))(lowess min95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray))(lowess max95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray)), graphregion(color(white)) legend(order(1 "Upper-middle class" 4 "Unskilled working class") nobox region(lstyle(none)) size(*1.4)) yscale(range(40 85)) xscale(range(2002 2016) noextend)  xlabel(2002 2007 2012 2016, labsize(*1.4)) ylabel(40(10)85, nogrid labsize(*1.4)) xtitle("")
graph export "$path\output\NO_margins_lowess_confint.png", replace

twoway (connected estimate year if class==1 & ed_level==3, sort lpattern (solid) lcolor(black) msymbol(circle) mcolor(black))(line min95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(line max95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(connected estimate year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(black) msymbol(triangle) mcolor(black))(line min95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray))(line max95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray)), graphregion(color(white)) legend(order(1 "Upper-middle class" 4 "Unskilled working class") nobox region(lstyle(none)) size(*1.4)) yscale(range(40 85)) xscale(range(2002 2016) noextend)  xlabel(2002 2007 2012 2016, labsize(*1.4)) ylabel(40(10)85, labsize(*1.4) nogrid format(%9.0f)) ytitle("") xtitle("")
graph export "$path\output\NO_margins_connected.png", replace


*DE
clear
use "$path\ESS_dataset_2002-2018_ready.dta"

fre YEAR if class5!=. & STFLIFE100!=. & COUNTRY==276


quietly reg STFLIFE100 class5##YEAR i.SEX01 AGE i.ed_level if COUNTRY==276 & AGE>=20 & AGE<=60
quietly margins YEAR, at (AGE=40 SEX01=1) by (class5 ed_level) post
parmest, format (estimate min95 max95 %8.3f p %8.1e) saving ("$path\margins_class5.dta", replace)

use "$path\margins_class5.dta", clear

br

split parm, p("." "#")
rename parm1 class5
destring class5, replace
rename parm3 ed_level
destring ed_level, replace
rename parm5 year
destring year, replace

twoway (lowess estimate year if class==1 & ed_level==3, sort lpattern (solid) lcolor(black))(lowess min95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(lowess max95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(lowess estimate year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(black))(lowess min95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray))(lowess max95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray)), graphregion(color(white)) legend(order(1 "Upper-middle class" 4 "Unskilled working class") nobox region(lstyle(none)) size(*1.4)) yscale(range(40 85)) xscale(range(2002 2016) noextend)  xlabel(2002 2007 2012 2016, labsize(*1.4)) ylabel(40(10)85, nogrid labsize(*1.4)) xtitle("")
graph export "$path\output\DE_margins_lowess_confint.png", replace

twoway (connected estimate year if class==1 & ed_level==3, sort lpattern (solid) lcolor(black) msymbol(circle) mcolor(black))(line min95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(line max95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(connected estimate year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(black) msymbol(triangle) mcolor(black))(line min95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray))(line max95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray)), graphregion(color(white)) legend(order(1 "Upper-middle class" 4 "Unskilled working class") nobox region(lstyle(none)) size(*1.4)) yscale(range(40 85)) xscale(range(2002 2016) noextend)  xlabel(2002 2007 2012 2016, labsize(*1.4)) ylabel(40(10)85, labsize(*1.4) nogrid format(%9.0f)) ytitle("") xtitle("")
graph export "$path\output\DE_margins_connected.png", replace


*CH
clear
use "$path\ESS_dataset_2002-2018_ready.dta"

fre YEAR if class5!=. & STFLIFE100!=. & COUNTRY==756

quietly reg STFLIFE100 class5##YEAR i.SEX01 AGE i.ed_level if COUNTRY==756 & AGE>=20 & AGE<=60
quietly margins YEAR, at (AGE=40 SEX01=1) by (class5 ed_level) post
parmest, format (estimate min95 max95 %8.3f p %8.1e) saving ("$path\margins_class5.dta", replace)

use "$path\margins_class5.dta", clear

br

split parm, p("." "#")
rename parm1 class5
destring class5, replace
rename parm3 ed_level
destring ed_level, replace
rename parm5 year
destring year, replace

twoway (lowess estimate year if class==1 & ed_level==3, sort lpattern (solid) lcolor(black))(lowess min95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(lowess max95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(lowess estimate year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(black))(lowess min95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray))(lowess max95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray)), graphregion(color(white)) legend(order(1 "Upper-middle class" 4 "Unskilled working class") nobox region(lstyle(none)) size(*1.4)) yscale(range(40 85)) xscale(range(2002 2016) noextend)  xlabel(2002 2007 2012 2016, labsize(*1.4)) ylabel(40(10)85, nogrid labsize(*1.4)) xtitle("")
graph export "$path\output\CH_margins_lowess_confint.png", replace

twoway (connected estimate year if class==1 & ed_level==3, sort lpattern (solid) lcolor(black) msymbol(circle) mcolor(black))(line min95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(line max95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(connected estimate year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(black) msymbol(triangle) mcolor(black))(line min95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray))(line max95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray)), graphregion(color(white)) legend(order(1 "Upper-middle class" 4 "Unskilled working class") nobox region(lstyle(none)) size(*1.4)) yscale(range(40 85)) xscale(range(2002 2016) noextend)  xlabel(2002 2007 2012 2016, labsize(*1.4)) ylabel(40(10)85, labsize(*1.4) nogrid format(%9.0f)) ytitle("") xtitle("")
graph export "$path\output\CH_margins_connected.png", replace



*PL
clear
use "$path\ESS_dataset_2002-2018_ready.dta"

fre YEAR if class5!=. & STFLIFE100!=. & COUNTRY==616

quietly reg STFLIFE100 class5##YEAR i.SEX01 AGE i.ed_level if COUNTRY==616 & AGE>=20 & AGE<=60
quietly margins YEAR, at (AGE=40 SEX01=1) by (class5 ed_level) post
parmest, format (estimate min95 max95 %8.3f p %8.1e) saving ("$path\margins_class5.dta", replace)

use "$path\margins_class5.dta", clear

br

split parm, p("." "#")
rename parm1 class5
destring class5, replace
rename parm3 ed_level
destring ed_level, replace
rename parm5 year
destring year, replace

twoway (lowess estimate year if class==1 & ed_level==3, sort lpattern (solid) lcolor(black))(lowess min95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(lowess max95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(lowess estimate year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(black))(lowess min95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray))(lowess max95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray)), graphregion(color(white)) legend(order(1 "Upper-middle class" 4 "Unskilled working class") nobox region(lstyle(none)) size(*1.4)) yscale(range(40 85)) xscale(range(2002 2016) noextend)  xlabel(2002 2007 2012 2016, labsize(*1.4)) ylabel(40(10)85, nogrid labsize(*1.4)) xtitle("")
graph export "$path\output\PL_margins_lowess_confint.png", replace

twoway (connected estimate year if class==1 & ed_level==3, sort lpattern (solid) lcolor(black) msymbol(circle) mcolor(black))(line min95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(line max95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(connected estimate year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(black) msymbol(triangle) mcolor(black))(line min95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray))(line max95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray)), graphregion(color(white)) legend(order(1 "Upper-middle class" 4 "Unskilled working class") nobox region(lstyle(none)) size(*1.4)) yscale(range(40 85)) xscale(range(2002 2016) noextend)  xlabel(2002 2007 2012 2016, labsize(*1.4)) ylabel(40(10)85, labsize(*1.4) nogrid format(%9.0f)) ytitle("") xtitle("")
graph export "$path\output\PL_margins_connected.png", replace


*HU
clear
use "$path\ESS_dataset_2002-2018_ready.dta"

fre YEAR if class5!=. & STFLIFE100!=. & COUNTRY==348


quietly reg STFLIFE100 class5##YEAR i.SEX01 AGE i.ed_level if COUNTRY==348 & AGE>=20 & AGE<=60
quietly margins YEAR, at (AGE=40 SEX01=1) by (class5 ed_level) post
parmest, format (estimate min95 max95 %8.3f p %8.1e) saving ("$path\margins_class5.dta", replace)

use "$path\margins_class5.dta", clear

br

split parm, p("." "#")
rename parm1 class5
destring class5, replace
rename parm3 ed_level
destring ed_level, replace
rename parm5 year
destring year, replace


twoway (lowess estimate year if class==1 & ed_level==3, sort lpattern (solid) lcolor(black))(lowess min95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(lowess max95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(lowess estimate year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(black))(lowess min95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray))(lowess max95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray)), graphregion(color(white)) legend(order(1 "Upper-middle class" 4 "Unskilled working class") nobox region(lstyle(none)) size(*1.4)) yscale(range(40 85)) xscale(range(2002 2016) noextend)  xlabel(2002 2007 2012 2016, labsize(*1.4)) ylabel(40(10)85, nogrid labsize(*1.4)) xtitle("")
graph export "$path\output\HU_margins_lowess_confint.png", replace


twoway (connected estimate year if class==1 & ed_level==3, sort lpattern (solid) lcolor(black) msymbol(circle) mcolor(black))(line min95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(line max95 year if class==1 & ed_level==3, sort lpattern (solid) lcolor(gray))(connected estimate year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(black) msymbol(triangle) mcolor(black))(line min95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray))(line max95 year if class==5 & ed_level==1, sort lpattern (shortdash) lcolor(gray)), graphregion(color(white)) legend(order(1 "Upper-middle class" 4 "Unskilled working class") nobox region(lstyle(none)) size(*1.4)) yscale(range(40 85)) xscale(range(2002 2016) noextend)  xlabel(2002 2007 2012 2016, labsize(*1.4)) ylabel(40(10)85, labsize(*1.4) nogrid format(%9.0f)) ytitle("") xtitle("")
graph export "$path\output\HU_margins_connected.png", replace


********************************************************************************
*Table W.1: Descriptive statistics of variables in analytical sample of ESS rounds 1-8 (2002-16), respondents aged 20-60

clear
use "$path\ESS_dataset_2002-2018_ready.dta"

tab class5 cntry if age>=20 & age<=60 & SEX01!=., col
tab ed_level cntry if age>=20 & age<=60 & SEX01!=., col
tab SEX01 cntry if age>=20 & age<=60 & SEX01!=., col

bys cntry: egen mean_STFLIFE100=mean(STFLIFE100) if age<=60 & SEX01!=.
tab cntry mean_STFLIFE100

bys cntry: egen mean_age=mean(age) if age<=60 & SEX01!=.
tab cntry mean_age
list cntry mean_age

fre cntry if stflife!=. & ed_level!=. & class5!=. & age>=20 & age<=60 & SEX01!=.

fre cntry if stflife!=. & ed_level!=. & class5!=. & age!=. & SEX01!=.